#ifndef _BASIC_UTILS_HH_
#define _BASIC_UTILS_HH_

#include <string>
#include <sstream>
#include <iostream>

#include <cxcore.h>
#include <cv.h>
#include <highgui.h>


template <class T>
bool from_string(T& t, 
                 const std::string& s, 
                 std::ios_base& (*f)(std::ios_base&) = std::dec)
{
  std::istringstream iss(s);
  return !(iss >> f >> t).fail();
}

template <typename T>
std::string to_string(const T &thing) {
    std::stringstream os;
    os << thing;
    return os.str();
}



// /** 
//  * Conversion is performed in-place.
//  * 
//  * @param img 
//  * @param depth 
//  * @param nChannels 
//  */
// void adapt_channels_depth(IplImage **src_img, int depth, int nChannels)
// {

//     IplImage *img = *src_img;

//     if(img->depth == depth && img->nChannels == nChannels)
// 	return;
    
//     if(nChannels != img->nChannels ){
// 	IplImage *aux = cvCreateImage(cvGetSize(img), img->depth, nChannels);
// 	int conv;
// 	if(nChannels == 3) conv = CV_GRAY2BGR;
// 	else conv = CV_BGR2GRAY;
// 	cvCvtColor(img, aux, conv);	    	    		
// 	cvReleaseImage(&img);
// 	img = aux;
//     }
//     if(depth != img->depth){
// 	IplImage *aux = cvCreateImage(cvGetSize(img), depth, nChannels);
// 	cvConvert(img, aux);
// 	cvReleaseImage(&img);
// 	img = aux;
//     }
   
//     *src_img = img;

//     return;
// }


void deleteIplImage(IplImage *img);
void deleteCvMat(CvMat *mat);

void deleteFileStorage(CvFileStorage *fs);

void deleteVideoWriter(CvVideoWriter *vw);

void deleteCvKalman(CvKalman *kalman);

void print_arr(const CvArr *arr, bool transpose = false, const char *format = NULL);


bool is_box_in_image(const CvRect r, const IplImage *img);

void AgruparMatriz(CvMat * mat, CvMat *small, CvMat *mask);
void AgruparMatriz(CvMat ** mat, CvMat *small, CvMat *mask);
CvMat * AgruparMatriz2new(CvMat * mat, CvMat *mask);
void AgruparMatriz(CvMat * mat, CvMat *mask);
void AddRow(CvMat * mat);
CvMat * AddRow2new(CvMat * mat);


#endif
